﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UI
{
    public partial class FSetBalance : Form
    {
        private int iAccountID;
        public FSetBalance()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 需要账户ID作为参数的构造函数
        /// </summary>
        /// <param name="accountID"></param>
        public FSetBalance(int accountID)
        {
            InitializeComponent();

            this.iAccountID = accountID;           //account.xml的accountID从1开始
            BLL.Account acc = new BLL.Account();
            iAccountID = accountID;
            acc = FMain.accMan.GetAccount(accountID);  //读取account.xml的AccountList
            //文本框数据初始化
            tbAccName.Text = acc.Name.ToString().Trim();
            tbAccName.Enabled = false;
            tbCurBalance.Text = acc.Balance.ToString().Trim();
            tbCurBalance.Enabled = false;
            tbNewBalance.Text = acc.Balance.ToString().Trim();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            int a = ModifyAccount(iAccountID);
            this.Close();
        }
        /// <summary>
        /// 余额调整时创建对账收入/支出记录，创建成功输入true，反之为false
        /// </summary>
        public bool CreateRecordForBal(double newBalance,double curBalance) 
        {
            double income = newBalance - curBalance;
            BLL.Record rec=new BLL.Record();
            try
           {
               int recoudCount = FMain.rcdMan.RecordList.Count;
               rec.ID = FMain.rcdMan.RecordList[recoudCount - 1].ID + 1;
                rec.Time = DateTime.Parse(System.DateTime.Now.ToString("d"));
                rec.AccountID = iAccountID;
                if (income > 0)
                {
                    rec.TypeID = 30;//测试用
                    rec.Amount = income;
                }
                else 
                {
                    rec.TypeID = 31;//测试用
                    rec.Amount = -income;

                }
                FMain.rcdMan.AddRecord(rec);
           }
           catch 
          {
                return false;
          }
            return true;
        }
        /// <summary>
        /// 余额调整时修改账户的余额信息
        /// </summary>
        public int ModifyAccount(int iAccountID)
        {
            BLL.Account acc = new BLL.Account();
            acc = FMain.accMan.GetAccount(iAccountID);
            if (tbNewBalance.Text!="")
            {

                try
                {
                    double curBalance=acc.Balance;
                    acc.Name = tbAccName.Text.ToString().Trim();
                    acc.Balance = double.Parse(tbNewBalance.Text.ToString().Trim());

                    if (curBalance == acc.Balance)
                    {
                        return 2;
                    }
                    else
                    {
                        if (!CreateRecordForBal(double.Parse(tbCurBalance.Text.ToString().Trim()), acc.Balance))
                        {
                            return 2;
                        }
                    }
                    acc.ModifiedTime = DateTime.Parse(System.DateTime.Now.ToString("d"));
                    FMain.accMan.UpdateAccount(acc);
                }
                catch
                {
                    return 2;
                }
                return 1;
            }
            return 0;


        }
    }
}
